package com.kma.summer2012.factory;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.beans.factory.access.BeanFactoryReference;
import org.springframework.beans.factory.access.SingletonBeanFactoryLocator;

public class SpringBeansFactory  {

    private static SpringBeansFactory instance;

    private final BeanFactory beanFactory;


    public static SpringBeansFactory getInstance(){
        if (instance == null) {
            instance = new SpringBeansFactory();
        }
        return instance;
    }

    private SpringBeansFactory() {

        BeanFactoryLocator beanFactoryLocator = SingletonBeanFactoryLocator.getInstance("classpath*:kma-beanRefFactory.xml");
        BeanFactoryReference beanFactoryReference = beanFactoryLocator.useBeanFactory("kma");

        beanFactory = beanFactoryReference.getFactory();
    }

    public <T> T getBeanByClass(Class<T> clazz) {
        return beanFactory.getBean(clazz);
    }

    public Object getBeanByName(String name) {
        return beanFactory.getBean(name);
    }

    public BeanFactory getBeanFactory() {
        return beanFactory;
    }

}
